package com.yizhu.zczy.system.service.impl;

import com.yizhu.zczy.common.utils.PageUtils;
import com.yizhu.zczy.common.utils.Query;
import com.yizhu.zczy.system.dao.PopedomDao;
import com.yizhu.zczy.system.entity.PopedomEntity;
import com.yizhu.zczy.system.entity.UserEntity;
import com.yizhu.zczy.system.interceptor.MyHandlerInterceptor;
import com.yizhu.zczy.system.vo.PermissionsVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.yizhu.zczy.system.dao.RoleRightsDao;
import com.yizhu.zczy.system.entity.RoleRightsEntity;
import com.yizhu.zczy.system.service.RoleRightsService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class RoleRightsServiceImpl extends ServiceImpl<RoleRightsDao, RoleRightsEntity> implements RoleRightsService {

    @Autowired
    private PopedomDao popedomDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RoleRightsEntity> page = this.page(
                new Query<RoleRightsEntity>().getPage(params),
                new QueryWrapper<RoleRightsEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 根据角色查询权限信息
     * @param roleId
     * @return
     */
    @Override
    public List<PermissionsVo> GetPermissionsByRole(Integer roleId) {
        QueryWrapper<RoleRightsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        List<RoleRightsEntity> list = this.list(queryWrapper);
        List<Integer> popedomIds = list.stream().map(RoleRightsEntity::getPopedomId).collect(Collectors.toList());
        List<PopedomEntity> popedomEntities = popedomDao.selectBatchIds(popedomIds);
        List<PermissionsVo> permissionsVos = popedomEntities.stream().map(item -> {
            PermissionsVo permissionsVo = new PermissionsVo();
            BeanUtils.copyProperties(item, permissionsVo);
            return permissionsVo;
        }).collect(Collectors.toList());
        return permissionsVos;
    }

    /**
     * 根据角色id修改权限的信息
     * @param roleId
     * @param popedomIds
     */
    @Override
    @Transactional
    public void modifyPermissionsByRoleId(Integer roleId, List<Integer> popedomIds) {
//        UserEntity userEntity = MyHandlerInterceptor.threadLocal.get();
        QueryWrapper<RoleRightsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        int count = this.count(queryWrapper);
        if (count>0){
           this.remove(new QueryWrapper<RoleRightsEntity>().eq("role_id",roleId));
        }
        List<RoleRightsEntity> rightsEntities = popedomIds.stream().map(popedomId -> {
            RoleRightsEntity roleRightsEntity = new RoleRightsEntity();
            roleRightsEntity.setRoleId(roleId).setPopedomId(popedomId);
//            roleRightsEntity.setCreateUserName(userEntity.getUserName()).setCreateUserId(userEntity.getUserId());
            return roleRightsEntity;
        }).collect(Collectors.toList());
        this.saveBatch(rightsEntities);
    }

}